package com.wen.service.Impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wen.common.R;
import com.wen.mapper.GatherMapper;
import com.wen.mapper.StockDetailsMapper;
import com.wen.pojo.Gather;
import com.wen.pojo.StockDetails;
import com.wen.pojo.dto.GatherDetailsDto;
import com.wen.pojo.vo.StockDetailsVo;
import com.wen.service.GatherService;
import com.wen.service.StockDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StockDetailsServiceImpl extends ServiceImpl<StockDetailsMapper, StockDetails> implements StockDetailsService {
    @Autowired
    private StockDetailsMapper mapper;
    @Override
    public Page<StockDetails> pageListStockDetails(StockDetailsVo stockDetailsVo, Long pageNum, Long pageSize) {
        Page<StockDetails> stockDetailsPage = new Page<>(pageNum, pageSize);
        Page<StockDetails> stockDetailsPage1 = mapper.pageListStockDetails(stockDetailsPage, stockDetailsVo);
        return stockDetailsPage1;
    }

    @Override
    @Transactional
    public List<GatherDetailsDto> GatherDetailsById(String id) {
        List<GatherDetailsDto> gatherDetailsDto = mapper.GatherDetailsById(id);
        for (GatherDetailsDto g:gatherDetailsDto) {
            Long stockId = g.getStockId();
            long productId = g.getProductId();
            long supplierId = g.getSupplierId();
            long amount = g.getAmount();
            StockDetails stockDetailss = mapper.stockId(stockId, productId, supplierId);
            StockDetails stockDetails = new StockDetails();
            if (stockDetailss == null) {
                stockDetails.setStockId(stockId);
                stockDetails.setProductId(productId);
                stockDetails.setSupplierId(supplierId);
                stockDetails.setAmount(amount);
                stockDetails.setRealAmount(amount);
                stockDetails.setStatus("c001-1");
                int insert = mapper.insert(stockDetails);
                if (insert > 0) {

                    System.out.println("添加成功");
                } else {
                    break;
                }
            } else {
                //获取id修改两个字段
                long ids = stockDetailss.getId();
                long amount1 = stockDetailss.getAmount();
                long realAmount = stockDetailss.getRealAmount();
                boolean flag = mapper.UpStockId(amount1, realAmount, ids);
                if (flag == true) {

                    boolean flag1 = mapper.UpStockIds(id);
                    if (flag1) {
                        System.out.println("修改成功");
                    } else {
                        System.out.println("修改失败");
                        break;
                    }

                    System.out.println("添加成功1");
                } else {
                    System.out.println("添加失败1");
                    break;
                }

//                long amount1 = stockDetails.getAmount();
//                long amounts = amount + amount1;
//                stockDetails.setAmount(amounts);
//                stockDetails.setRealAmount(amounts);
//                stockDetails.setStatus("c001-1");
//                int insert = mapper.updateById(stockDetails);
//                if (insert > 0) {
//                    System.out.println("添加成功1");
//                } else {
//                    break;
//                }
            }
        }
        return gatherDetailsDto;
    }
}
